/**
 *
 */
package com.ssyt.examinationsystem.service.impl;

import com.ssyt.examinationsystem.dao.IRoleInfoDAO;
import com.ssyt.examinationsystem.dao.ITeacherInfoDAO;
import com.ssyt.examinationsystem.dao.impl.RoleInfoDAOImpl;
import com.ssyt.examinationsystem.dao.impl.TeacherInfoDAOImpl;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.service.ITeacherInfoService;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 教师业务层接口的实现类
 *
 * @author Administrator
 */
public class TeacherInfoServiceImpl implements ITeacherInfoService {
    ITeacherInfoDAO teaDao = new TeacherInfoDAOImpl();
    private IRoleInfoDAO roleDao = new RoleInfoDAOImpl();

    /**
     * 管理员和教师登录的业务方法
     */
    public boolean login(TeacherInfo teacher) {
        return teaDao.login(teacher);
    }

    @Override
    public List<TeacherInfo> getTeacherInfos() {
        // 调用数据处理层的查询方法
        List<Map<String, Object>> list = teaDao.getTeacherInfos();
        // 创建一个存放教师对象的集合
        List<TeacherInfo> teaList = new ArrayList<TeacherInfo>();
        if (CollectionUtils.isEmptyForList(list)) {
            for (Map<String, Object> map : list) {
                // 创建一个教师对象，并封装属性值
                TeacherInfo tea = new TeacherInfo();
                tea.setTeaId(Integer.valueOf(String.valueOf(map.get("teaId"))));
                tea.setAccount(String.valueOf(map.get("account")));
                tea.setPassword(String.valueOf(map.get("password")));
                RoleInfo role = new RoleInfo();
                String roleId = String.valueOf(map.get("roleId"));
                // 根据角色编号的到角色对象
                Map<String, Object> mapRole = roleDao.getRoleByRoleId(roleId);
                role.setRoleName(String.valueOf(mapRole.get("roleName")));
                role.setRoleId(Integer.valueOf(roleId));
                tea.setRole(role);
                tea.setTeaName(String.valueOf(map.get("teaName")));
                // 把封装好的教师对象添加到集合中
                teaList.add(tea);
            }
        }
        return teaList;
    }

    @Override
    public int deleteTeacherInfo(String teaId) {
        return teaDao.deleteTeacherInfo(teaId);
    }

    @Override
    public int addTeacherInfo(TeacherInfo tea) {
        return teaDao.addTeacherInfo(tea);
    }

    @Override
    public int updateTeacherInfo(TeacherInfo tea) {
        return teaDao.updateTeacherInfo(tea);
    }

    @Override
    public Map<String, Object> getTeaByStuId(String teaId) {
        return teaDao.getTeaByStuId(teaId);
    }

}
